﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

using BackgroundTest.BasicAbstracts;
using BackgroundTest.Sprites;

namespace BackgroundTest.LogicInterface
{
    class LogicBuffer
    {
        // The thread to conduct:
        Thread logicBuffer;

        // The lock object
        object lockObject;

        /************************************************************************/
        /* Method name: Construction Function                                   */
        /* Parameters: Camera: the camera of the battle field.                  */
        /************************************************************************/
        public LogicBuffer()
        {
            lockObject = new Object();
            BackgroundTest.BackGroundTest.Console.WriteLine("@Logic Buffer>  Registering...");
            LuaFramework.Instance.registerLuaFunctions(this);
            BackgroundTest.BackGroundTest.Console.WriteLine("@Logic Buffer>  Register finished.");
            logicBuffer = new Thread(new ThreadStart(this.BufferStartUp));
            logicBuffer.Start();
            BackgroundTest.BackGroundTest.Console.WriteLine("@Logic Buffer>  Logic Buffer set up.");
        }

        /************************************************************************/
        /* Method name: BufferStartUp                                           */
        /************************************************************************/
        public void BufferStartUp()
        {
            LuaFramework.Instance.LuaLogicVM.DoFile(@"./Lua/Logic/test.lua");         
            BackgroundTest.BackGroundTest.Console.WriteLine("@Logic Buffer>  Logic ends here.");
        }

        /************************************************************************/
        /* Method name: ForceTerminate                                          */
        /************************************************************************/
        public void ForceTerminate()
        {
            logicBuffer.Abort();
        }

        /// <summary>
        /// Logic sleep function, the logic will sleep param=frame frames.
        /// </summary>
        /// <param name="frame">The number of frame that would sleep</param>
        [AttrLuaFunc("LogicSleep", "Logic sleep function, the logic will sleep param=frame frames.",
            "The number of frame that would sleep")]
        public void LogicSleep(int frame)
        {
            Thread.Sleep(Convert.ToInt16(frame * 1.0f/BasicData.FramePerSecond * 1000));
        }
    }
}
